<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Models\UserCapital;
use App\Http\Models\UserSubmitAudit;
use App\Http\Models\User;
use App\Http\Models\UserAuth;
use App\Http\Models\UserAppKey;
use App\Http\Models\VoucherLog;
use App\Http\Models\StatisticsUser;
use App\Http\Models\UserIntegralLog;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class importuser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量导入用户';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle()
    {

        $get_user = DB::table('users')->orderBy('id', 'desc')->first();
        if ($get_user && $get_user->id < 10000) {
            DB::connection('mysql')->select("alter table users AUTO_INCREMENT=10000");
        }
        $users = DB::connection('mysql_old')->select("SELECT * FROM  rm_users WHERE `user_type` =2 AND  `mobile`>0 ");

        foreach ($users as $key => $value) {
            $is_set_user = User::where(['phone' => $value->mobile])->first(); //手机号去重
            if ($is_set_user) {
                continue;
            }
            if ($value->avatar) {
                $u = DB::table('users_copy_bak')->where(['old_id' => $value->id])->where('id', '>', 9999)->first();
                if ($u) {
                    $avater = $u->avatar;
                } else {
                    $upload_image = env('OLD_HTTP_URL') . $value->avatar;
                    $image_url = \App\Http\Controllers\ImageController::uploadUrlImageOne($upload_image);
                    $avater = $image_url ? $image_url : '';
                }
            } else {
                $avater = env('IMAGE_HTTP_URL').env('User_Default_Avatar');
            }
            $user_id = User::insertGetId([
                'phone' => $value->mobile,
                'name' => $value->user_nicename,
                'email' => $value->user_email,
                'password' => Hash::make(substr($value->user_pass, 3, strlen($value->user_pass) - 1)),
                'signature' => $value->signature,
                'qq' => '',
                'avatar' => $avater,
                'created_at' => $value->create_time,
                'old_id' => $value->id,
            ]);
            if ($value->canfangdan == 1) {
                $uc = UserCapital::insert([
                    'voucher_number' => env('Voucher_Default_Give'),
                    'user_id' => $user_id,
                    'integral' => $value->jifen,
                    'created_at' => date('Y-m-d H:i:s', time())
                ]);
                if ($value->jifen) {
                    UserIntegralLog::addUserIntegralLog($user_id, '初始化积分为20', env('UserIntegral'), 0, 1, $refuse = '');
                }
                if ($uc) {
                    VoucherLog::addUserVoucherLog($user_id, '初始发放' . env('Voucher_Default_Give') . '点券', env('Voucher_Default_Give'), 2, 0, '');
                }
                $users_audit = DB::connection('mysql_old')->select("SELECT * FROM  rm_fangdaninfo  WHERE `uid`={$value->id} order by  id desc limit 1;");
                if ($users_audit) {
                    $users_audit = $users_audit[0];
                    UserSubmitAudit::insert([
                        'uid' => $user_id,
                        'state' => $users_audit->status,
                        'qq' => $users_audit->contactqq,
                        'content' => $users_audit->desc,
                        'phone' => $value->mobile,
                        'created_at' => date('Y-m-d H:i:s', time())
                    ]);
                }
            }
            if ($value->appkey) {
                UserAppKey::insert([
                    'appkey' => $value->appkey,
                    'user_id' => $user_id,
                    'created_at' => date('Y-m-d H:i:s', time()),
                    'phone' => $value->mobile
                ]);
            }
            echo "更新用户" . $user_id . "数据\n";

        }
        $all_number = User::count();
        $all_auth_number = UserAuth::where(['status' => 1])->count();
        $user_submit_audit = UserSubmitAudit::where(['state' => 1])->count();
        $user_model = StatisticsUser::first();
        if (empty($user_model)) {
            $user_model = new StatisticsUser();
        }
        $user_model->all_number = $all_number;
        $user_model->is_auth = $all_auth_number;
        $user_model->order_permissions = $user_submit_audit;
        $user_model->save();
        Log::info('更新 old user' . $user_id);
    }
}
